package com.qianyu.controller.comm;

import com.qianyu.anno.OperLogAnnotation;
import com.qianyu.entity.base.BaseNotifyDto;
import com.qianyu.entity.base.Result;
import com.qianyu.entity.dto.GiveHbNotifyDto;
import com.qianyu.entity.dto.QqAccountDto;
import com.qianyu.entity.dto.QunDto;
import com.qianyu.entity.vo.QqAccountVo;
import com.qianyu.entity.vo.QqMemberVo;
import com.qianyu.entity.vo.QunMemberKeepQhVo;
import com.qianyu.entity.vo.QunVo;
import com.qianyu.enumerate.OperLogEnum;
import com.qianyu.enumerate.ResultEnum;
import com.qianyu.exception.CustomerException;
import com.qianyu.service.QqAccountService;
import com.qianyu.service.QunMemberService;
import com.qianyu.service.QunService;
import com.qianyu.util.CacheKeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "公共管理", description = "wangzhicheng")
@RestController
@RequestMapping("/api/common")
@RequiredArgsConstructor
public class CommonController {

    private final QqAccountService qqAccountService;

    private final QunService qunService;

    private final QunMemberService qunMemberService;

    private final RedissonClient redissonClient;

    /**
     * 获取未实名账号
     */
    @GetMapping("/getNoAuthAccount")
    @ApiOperation(value = "获取未实名任务")
    Result<QqAccountVo.QqAccountSimpleVo> getNoAuthAccount() {
        return Result.success(qqAccountService.getNoAuthAccount());
    }

    /**
     * qq认证回调方法
     *
     * @param dto
     * @return
     */
    @PostMapping("/qq_auth_notify")
    @ApiOperation(value = "qq认证回调方法")
    @OperLogAnnotation(menuName = "qq认证回调方法", opeType = OperLogEnum.UPDATE)
    Result<String> authAccount(@RequestBody @Validated QqAccountDto.QqAccountAuthDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.notifyLockKey(dto.getQq()));
        boolean flag;
        if (lock.tryLock()) {
            try {
                flag = qqAccountService.authAccount(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new CustomerException(ResultEnum.NOTIFICATION_SERVICE_ERROR);
        }
        return flag ? Result.success() : Result.error();
    }


    /**
     * 建群取号
     *
     * @return
     */
    @GetMapping("/getAuthQun")
    @ApiOperation(value = "建群取号任务")
    Result<QunVo.QunSimpleVo> getAuthQun() {
        return Result.success(qunService.getAuthQun());
    }

    /**
     * 建群回调
     *
     * @param dto
     * @return
     */
    @PostMapping("/qun_auth_notify")
    @ApiOperation(value = "建群回调")
    @OperLogAnnotation(menuName = "建群回调", opeType = OperLogEnum.UPDATE)
    Result<String> qunNotify(@RequestBody @Validated QunDto.QunNotifyDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.createQunLockKey(dto.getId()));
        boolean flag;
        if (lock.tryLock()) {
            try {
                flag = qunService.qunNotify(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new CustomerException(ResultEnum.NOTIFICATION_SERVICE_ERROR);
        }

        return flag ? Result.success() : Result.error();
    }

    /**
     * 加群取号
     */
    @GetMapping("/joinQun")
    @ApiOperation(value = "加群取号任务")
    Result<QqMemberVo> joinQun() {
        return Result.success(qunMemberService.joinQun());
    }

    /**
     * 加群回调
     *
     * @param dto
     * @return
     */
    @PostMapping("/join_notify")
    @ApiOperation(value = "加群回调")
    @OperLogAnnotation(menuName = "加群回调", opeType = OperLogEnum.UPDATE)
    Result<String> qunNotify(@RequestBody @Validated BaseNotifyDto dto) {


        RLock lock = redissonClient.getLock(CacheKeyUtil.notifyLockKey(dto.getQq()));
        boolean flag;
        if (lock.tryLock()) {
            try {
                flag = qunMemberService.joinNotify(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new CustomerException(ResultEnum.NOTIFICATION_SERVICE_ERROR);
        }

        return flag ? Result.success() : Result.error();
    }


    /**
     * 发红包保持取号
     */
    @GetMapping("/keepQh")
    @ApiOperation(value = "发红包保持取号")
    Result<QunMemberKeepQhVo> keepQh() {
        return Result.success(qunMemberService.keepQh());
    }

    /**
     * 保持取号回调
     *
     * @param dto
     */
    @PostMapping("/keep_qh_notify")
    @ApiOperation(value = "保持取号回调")
    @OperLogAnnotation(menuName = "保持取号回调", opeType = OperLogEnum.UPDATE)
    Result<String> keepQhNotify(@RequestBody @Validated BaseNotifyDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.notifyLockKey(dto.getQq()));
        boolean flag;
        if (lock.tryLock()) {
            try {
                flag = qunMemberService.keepQhNotify(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new CustomerException(ResultEnum.NOTIFICATION_SERVICE_ERROR);
        }

        return flag ? Result.success() : Result.error();
    }


    /**
     * 发红包回调
     *
     * @param dto
     */
    @PostMapping("/give_hb_notify")
    @ApiOperation(value = "发红包回调")
    @OperLogAnnotation(menuName = "发红包回调", opeType = OperLogEnum.UPDATE)
    Result<String> giveHbNotify(@RequestBody @Validated GiveHbNotifyDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.notifyLockKey(dto.getQq()));
        boolean flag;
        if (lock.tryLock()) {
            try {
                flag = qunMemberService.giveHbNotify(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new CustomerException(ResultEnum.NOTIFICATION_SERVICE_ERROR);
        }

        return flag ? Result.success() : Result.error();
    }

}
